//
//  ByMyFocusVC.m
//  caipiao_ios
//
//  Created by mc on 2018/9/4.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByMyFocusVC.h"
#import "ByDocumentaryUserVC.h"

@interface ByMyFocusVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UILabel *valueLabel;

@end

@implementation ByMyFocusVC

- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray arrayWithCapacity:1];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addLeftBackBarButtonItem];
    
    [self initViews];
    
    ByCoreRefreshGifHeader *header = [ByCoreRefreshGifHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];
    self.tableView.mj_header = header;
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    
    [self performSelector:@selector(loadData) withObject:nil afterDelay:0.1];
}


- (void)loadData{
    
    ByNetModel *netModel = [[ByNetModel alloc] init];
    netModel.follow = @"0";
    self.title = @"我的关注";
    
    [ByCoreToolAlert showLoadind:@"加载中" hasMasker:YES];
    [ByDocumentaryApi followOrderFollowList:netModel success:^(ByNetBaseResult *result) {
        [ByCoreToolAlert hideHud];
        [self.tableView.mj_header endRefreshing];
        NSLog(@"我的关注  = %@", result.data);
        [self.dataArray removeAllObjects];
        for (NSDictionary *dic in result.data[@"data"][@"list"]) {
            ByNetModel *netModel = [[ByNetModel alloc] initContentWithDic:dic];
            [self.dataArray addObject:netModel];
        }
        self.valueLabel.text = [NSString stringWithFormat:@"合计：%ld人", self.dataArray.count];
        [self.tableView reloadData];
        
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showFailureWithText:result.msg];
        [self.tableView.mj_header endRefreshing];
    }];
    
}


- (void)initViews{
    
    self.view.backgroundColor = BY_BACKGROUND_COLOR;
    self.tableView = [[UITableView alloc] init];
    self.tableView.backgroundColor = BY_BACKGROUND_COLOR;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        if (@available(ios 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(0);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-49);
        } else {
            make.top.equalTo(self.view).offset(0);
            make.bottom.equalTo(self.view).offset(-49);
        }
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    UILabel *titleLabel = [[UILabel alloc] init];
    [footer addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(footer).offset(0);
        make.left.equalTo(footer).offset(0);
        make.right.equalTo(footer).offset(0);
        make.height.mas_equalTo(@15);
    }];
    titleLabel.font = FONT(12);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"暂无更多数据";
    titleLabel.textColor = [UIColor grayColor];
    self.tableView.tableFooterView = footer;
    
    UILabel *valueLabel = [[UILabel alloc] init];
    [self.view addSubview:valueLabel];
    [valueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.top.equalTo(self.tableView.mas_bottom).offset(0);
        make.height.mas_equalTo(@49);
    }];
    valueLabel.backgroundColor = BY_THEME_COLOR;
    valueLabel.textColor = [UIColor whiteColor];
    valueLabel.font = FONT(14);
    valueLabel.textAlignment = NSTextAlignmentCenter;
    self.valueLabel = valueLabel;
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.contentView.backgroundColor = BY_BACKGROUND_COLOR;
        
        UILabel *line = [[UILabel alloc] init];
        [cell addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(cell).offset(0);
            make.right.equalTo(cell).offset(0);
            make.bottom.equalTo(cell).offset(0);
            make.height.mas_equalTo(@1);
        }];
        line.backgroundColor = BY_LINE_COLOR;
        
    }
    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    

    UIImageView *iconView = [[UIImageView alloc] init];
    [cell.contentView  addSubview:iconView];
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cell.contentView).offset(10);
        make.centerY.equalTo(cell.contentView).offset(0);
        make.size.mas_equalTo(CGSizeMake(50, 50));
    }];
    
    iconView.layer.cornerRadius = 25;
    iconView.layer.masksToBounds = YES;
    iconView.userInteractionEnabled = YES;
    iconView.tag = 200 + indexPath.row;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [iconView addGestureRecognizer:tap];
    
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cell.contentView addSubview:cancelBtn];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(cell.contentView).offset(-16);
        make.centerY.equalTo(cell.contentView).offset(0);
        make.size.mas_equalTo(CGSizeMake(80, 29));
    }];
    cancelBtn.titleLabel.font = FONT(14);
    cancelBtn.backgroundColor = BY_THEME_COLOR;
    [cancelBtn setTitle:@"取消关注" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    cancelBtn.tag = 100 + indexPath.row;
    [cancelBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    [cell.contentView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconView.mas_right).offset(10);
        make.centerY.equalTo(cell.contentView).offset(0);
        make.height.mas_equalTo(@20);
        make.right.equalTo(cancelBtn.mas_left).offset(-10);
    }];
    titleLabel.font = FONT(14);
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.textAlignment = NSTextAlignmentLeft;
    
    ByNetModel *netModel = [self.dataArray objectAtIndex:indexPath.row];
    titleLabel.text = netModel.nickname;
    [iconView sd_setImageWithURL:[NSURL URLWithString:netModel.head_pic] placeholderImage:HEAD_ICOM_IMAGE];
    
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}

- (void)tapAction:(UITapGestureRecognizer *)tap{
    ByNetModel *model = [self.dataArray objectAtIndex:tap.view.tag - 200];
    ByNetModel *netModel = [[ByNetModel alloc] init];
    netModel.uid = model.ID;
    
    ByDocumentaryUserVC *vc = [[ByDocumentaryUserVC alloc] init];
    vc.netModel = netModel;
    [self by_normalPush:vc hasNavi:YES];
    
}


- (void)btnAction:(UIButton *)btn{
    
    ByNetModel *model = [self.dataArray objectAtIndex:btn.tag - 100];
    ByNetModel *netModel = [[ByNetModel alloc] init];
    [ByCoreToolAlert showLoadind:@"处理中"hasMasker:YES];
    netModel.uid = model.ID;
    netModel.status = @"2";
    [ByDocumentaryApi followOrderFollow:netModel success:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showSuccessWithText:@"操作成功"];
        [self loadData];
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showFailureWithText:result.msg];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
